package com.just.just_bankreputationsystem.Controller;

import com.just.just_bankreputationsystem.Model.User;
import com.just.just_bankreputationsystem.Service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("user")
public class UserController {
    @Autowired
    private UserService userService;
    //看到页面
    @GetMapping("/login")
    public String login(){
        return "user/login";
    };
    //执行登录
    @PostMapping("/login")
    public String login(User user, HttpSession session, Model model){
        User dbuser= userService.findUser(user);
        if(dbuser!=null){
            //登录成功需要记录已经登陆
            session.setAttribute("user",dbuser);
            return "redirect:/BankReputation/index";
        }else {
            model.addAttribute("msg","用户名或密码错误！");
            model.addAttribute("user",user);
            return "user/login";
        }
    }
    @GetMapping("/exit")
    public String exit(HttpSession session){
        session.removeAttribute("user");
        return "redirect:/user/login";
    }
}
